//
//  ST_PairsPresentAnimator.swift
//  btvideo
//
//  Created by mac on 2020/1/2.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit

class ST_PairsPresentAnimator: NSObject,UIViewControllerAnimatedTransitioning{
 
    
    var operationView:ST_PairsShareParticularView
    
    init(sharedPlayView:ST_PairsShareParticularView) {
        operationView = sharedPlayView
        super.init()
    }
    
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.25
    }
    
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
//        UIViewPropertyAnimator
        
        guard let nav = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? UINavigationController ,let centerVC = nav.topViewController as? ST_PairsPresenAnimatorProtocol else{ return }
        
        let container = transitionContext.containerView
        
        let playerView = operationView
        
        playerView.removeFromSuperview()
        
        container.addSubview(playerView)
        
        playerView.frame = playerView.present_start_rect
        
        var fullScreenHeight = playerView.height*ScreenWidth/playerView.width
        
        if fullScreenHeight > PairsShareHeaderMaxHeight{
           fullScreenHeight = PairsShareHeaderMaxHeight
        }
        
        let endRect = CGRect.init(x: 0, y: statusBarHeight, width: ScreenWidth, height: fullScreenHeight)
        
//        print("结束之后放大到:",endRect)
        
//        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.8, options: [], animations: {
        
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            
            playerView.frame = endRect
//            playerView.rightSignLabel.alpha = 0
        }) { (f) in
            
            playerView.isUserInteractionEnabled = true
            
            container.addSubview(nav.view)
            
            playerView.removeFromSuperview()
//
            playerView.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: fullScreenHeight)
//
            centerVC.package_header_play_view(playerView)
            
            transitionContext.completeTransition(true)
        }
        
        
    }
    

    
    
    
}


//MARK: - Protocol


protocol ST_PairsPresenAnimatorProtocol{
    
    func package_header_play_view(_ view:ST_PairsShareParticularView)
    
}
